import { listByTab, listOrder } from "@/api/order/order";
import { ORDER_LIST, ORDER_TAB_LIST } from "@/constant/URL";
import { timestampToDate } from "@/utils/time";

/**
 * 订单列表
 */
class OrderComponentList {
  component;
  /**
   * @param  {import("vue").Component}component
   */
  constructor(component) {
    this.component = component;
  }

  /** 查询订单列表 */
  getList() {
    let params = {};
    if (this.component.isSearch) {
      params = Object.assign({}, this.component.queryParams);
      if(this.component.dateRange && 2 == this.component.dateRange.length){
        params['params'] = {
          startTime: new Date(this.component.dateRange[0]).getTime() / 1000,
          endTime: new Date(this.component.dateRange[1]).getTime() / 1000
        }
      }
      
    } else {
      params = {
        pageNum: this.component.queryParams.pageNum,
        pageSize: this.component.queryParams.pageSize,
      };
    }
    this.common(params, ORDER_LIST);
  }

  all() {
    this.common(this.component.queryParams, ORDER_LIST);
  }

  /**
   * 合并两个数字
   * @param {number} cmd
   * @param {number} subcmd
   * @returns
   */
  merge(cmd, subcmd) {
    return (cmd << 16) + subcmd;
  }

  /**
   * 待定价
   */
  awaitingQuote() {
    this.tabParse(
      {
        pageNum: this.component.queryParams.pageNum,
        pageSize: this.component.queryParams.pageSize,
        id: [0, 1],
      },
      ORDER_TAB_LIST
    );
  }

  /**
   * 待支付
   */
  awaitingPayment() {
    this.tabParse({
      pageNum: this.component.queryParams.pageNum,
      pageSize: this.component.queryParams.pageSize,
      id: [2, 3],
    });
  }
  /**
   * 已完成
   */
  completed() {
    this.tabParse({
      pageNum: this.component.queryParams.pageNum,
      pageSize: this.component.queryParams.pageSize,
      id: [11],
    });
  }

  cancelled() {
    this.tabParse({
      pageNum: this.component.queryParams.pageNum,
      pageSize: this.component.queryParams.pageSize,
      id: [-1],
    });
  }

  /**
   * 待审核
   */
  awaitingApproval() {
    this.tabParse({
      pageNum: this.component.queryParams.pageNum,
      pageSize: this.component.queryParams.pageSize,
      id: [8, 9],
    });
  }

  /**
   * 处理中
   */
  inProgress() {
    this.tabParse({
      pageNum: this.component.queryParams.pageNum,
      pageSize: this.component.queryParams.pageSize,
      id: [4, 5, 6, 7],
    });
  }

  /**
   *
   * @param {{}} param
   */
  common(param, url) {
    this.component.loading = true;
    listOrder(param, url).then((response) => {
      this.parseData(response);
    });
  }

  tabParse(param) {
    listByTab(param).then((res) => this.parseData(res));
  }

  parseData(response) {
    let orderList = response.rows;

    // 循环 处理 parseTime
    for (let i = 0; i < orderList.length; i++) {
      orderList[i].createTime = timestampToDate(orderList[i].createTime);
      if (0 == orderList[i].parseTime || null == orderList[i].parseTime) {
        orderList[i].parseTime = "";
        continue;
      }
      orderList[i].parseTime = timestampToDate(orderList[i].parseTime);
    }
    this.component.orderList = orderList;
    this.component.total = response.total;
    this.component.loading = false;
  }
}

export { OrderComponentList };
